package main

import "fmt"

// 声明一个包含 2 的幂次方的切片
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {

	for i, v := range pow {
		fmt.Printf("2**%d=%d\n", i, v)
	}

	for i, c := range "hello" {
		fmt.Printf("index:%d, char:%c\n", i, c)
	}

	map1 := make(map[int]float32)
	map1[1] = 1.0
	map1[2] = 2.0
	map1[3] = 3.0
	map1[4] = 4.0
	map1[5] = 5.0

	// 遍历map1 ，读取key 和 value
	for key, value := range map1 {
		fmt.Printf("key is: %d - value is: %f\n", key, value)
	}

	//只遍历map1 的key
	for key := range map1 {
		fmt.Printf("key is: %d \n", key)
	}

	// 遍历map1 , 只读取 value
	for _, value := range map1 {
		fmt.Printf("value is: %f\n", value)
	}

	// 通道（Channel）
	ch := make(chan int, 2)
	ch <- 1
	ch <- 2
	close(ch)

	for v := range ch {
		fmt.Println(v)
	}

	// 忽略值

	nums := []int{1, 2, 3, 4}

	for _, num := range nums {
		fmt.Println("value:", num)
	}

	for i := range nums {
		fmt.Println("index:", i)
	}

	// range 求 slice 的和
	numbers := []int{1, 3, 4, 5}
	sum := 0

	for _, num := range numbers {
		sum += num
	}
	fmt.Println("sum:", sum)

	for i, num := range nums {
		if num == 3 {
			fmt.Println("index:", i)
		}
	}

	kvs := map[string]string{"a": "apple", "b": "banana"}

	for k, v := range kvs {
		fmt.Printf("%s -> %s\n", k, v)
	}

	for i, c := range "go" {
		fmt.Println(i, c)
	}

}
